package com.eascs.web.demo.dubbo.action;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.site.controller.BaseViewController;
import com.eascs.site.view.ViewContext;
import com.eascs.web.demo.db.DbDemoEntity;
import com.eascs.web.demo.db.DbDemoForm;
import com.eascs.web.demo.dubbo.service.DubboDemoService;
import com.eascs.web.exception.RedirectException;

@Controller
public class DubboDemoViewController extends BaseViewController {
	private final Logger logger = LoggerFactory.getLogger(DubboDemoViewController.class);

	@Inject
	private DubboDemoService dubboDemoService;


	@RequestMapping(value = "/demo/dubbo/db/")
	public String index() {
		return "/sample/db/index";
	}

	@RequestMapping(value = "/demo/dubbo/db/transactional/true")
	public String testTransactionalSuccess() {
		dubboDemoService.testTransactional(true);
		throw new RedirectException("/demo/dubbo/db/list");
	}

	@RequestMapping(value = "/demo/dubbo/db/transactional/false")
	public String testTransactionalFail() {
		dubboDemoService.testTransactional(false);
		throw new RedirectException("/demo/dubbo/db/list");
	}

	@RequestMapping(value = "/demo/dubbo/db/clean")
	public String clean() {
		dubboDemoService.clean();
		throw new RedirectException("/demo/dubbo/db/");
	}

	@RequestMapping(value = "/demo/dubbo/db/init")
	public String init() {
		dubboDemoService.init();
		throw new RedirectException("/demo/dubbo/db/");
	}

	@RequestMapping(value = "/demo/dubbo/db/list")
	public String list() {
		ViewContext.put("objs", dubboDemoService.list());
		return "/sample/db/list";
	}

	@RequestMapping(value = "/demo/dubbo/db/add")
	public String add(DbDemoForm dbDemoForm) {
		DbDemoEntity dbDemoEntity = new DbDemoEntity();
		dbDemoEntity.setId(dbDemoForm.getId());
		dbDemoEntity.setName(dbDemoForm.getName());
		dbDemoEntity.setAddress(dbDemoForm.getAddress());
		dubboDemoService.add(dbDemoEntity);
		throw new RedirectException("/demo/dubbo/db/list");
	}

}